Ext.define('Admin.view.order.OrderViewController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.order',
    onClickOrderGridAddButton: function (view) {
    		Ext.create(Ext.apply({xtype: 'order-add-window'}));
			var json=Ext.util.JSON.decode(userInfo);
			var form=Ext.getCmp('order-add-form').getForm();
			form.findField('departmentName').setValue(json.dept);
    },
    onClickAddFormSubmitButton: function (btn) {
    	var form = btn.up('form');
		form.getForm().submit({
            params: {
                "_csrf": document.getElementsByTagName('meta')['_csrf'].content
            },
            failure: function(form, action) {
                Ext.Msg.alert('错误', response.status + ' ' + response.statusText);
            },     
			url:'order/save',
			success: function(form, action){    
				Ext.Msg.alert('Success', action.result.msg,function(){
					btn.up('window').close();
					Ext.getCmp('orderGrid').getStore().reload();
				});       
			},       
			failure: function(form, action){
				Ext.Msg.alert('Error', action.result.msg);
			}
		});
    },
	onClickOrderGridUpdateButton: function (btn) {
    	var grid = btn.up('gridpanel');//获取Grid视图
		var selModel = grid.getSelectionModel();//获取Grid的SelectionModel
		if (selModel.hasSelection()) {//判断是否选中记录
			if ((selModel.getSelection()[1]) != null){
				Ext.Msg.alert('提示',"请选择其中一行数据进行编辑!");
			}else{
			   var record = selModel.getSelection()[0];//获取选中的第一条记录
			   //创建修改window和form
			   var orderGridWindow = Ext.create('order-update-window',{
					title:'修改订餐记录',
					items: [{xtype: 'order-update-form'}]
				});
					//让form加载选中记录
			   orderGridWindow.down("form").getForm().loadRecord(record);
			}
		}else{
        	Ext.Msg.alert('提示',"请选择一行数据进行编辑!");
        }
    },
	onClickUpdateFormSubmitButton: function (btn) {
    	var form = btn.up('form');
		form.getForm().submit({
            params: {
                "_csrf": document.getElementsByTagName('meta')['_csrf'].content
            },
            failure: function(form, action) {
                Ext.Msg.alert('错误', response.status + ' ' + response.statusText);
            },       
			url:'order/update',
			success: function(form, action){    
				Ext.Msg.alert('Success', action.result.msg,function(){
					btn.up('window').close();
					Ext.getCmp('orderGrid').getStore().reload();
					Ext.getCmp('orderapprove-grid').getStore().reload();
				});       
			},       
			failure: function(form, action){
				Ext.Msg.alert('Error', action.result.msg);
			}
		});
    },
	onClickOrderGridDeleteButton: function (btn) {
		var grid = btn.up('gridpanel');
		var selModel = grid.getSelectionModel();
		if (selModel.hasSelection()) {
			Ext.Msg.confirm("警告", "确定要删除吗？", function (button) {
				if (button == "yes") {
					var selected = selModel.getSelection();
					var selectOrderIds = []; //要删除的orderId
					Ext.each(selected, function (item) {
						selectOrderIds.push(item.data.orderId);
					})
					Ext.Ajax.request({
						method: 'post',
						defaultHeaders: {
							"X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
						}, 
						url : 'order/delete', 
						params : { 
							//ids[] :selectIds
							orderIds :selectOrderIds
						}, 
						success: function(response, options) {
							var json = Ext.util.JSON.decode(response.responseText);
							if(json.success){
								Ext.Msg.alert('操作成功', json.msg, function() {
									grid.getStore().reload();
								});
							}else{
								 Ext.Msg.alert('操作失败', json.msg);
							}
						}
					});

				}
			});
		}
		else {
			Ext.Msg.alert("错误", "没有任何行被选中，无法进行删除操作！");
		}
    }
   
	,onClickStartButton: function (btn) {
		var grid = btn.up('gridpanel');//获取Grid视图
		var selModel = grid.getSelectionModel();//获取Grid的SelectionModel
		if (selModel.hasSelection()) {//判断是否选中记录
			if ((selModel.getSelection()[1]) != null){
				Ext.Msg.alert('提示',"请选择其中一行数据进行编辑!");
			}else{
				var record = selModel.getSelection()[0];//获取选中的第一条记录
				Ext.Ajax.request({
					method: 'post',
					defaultHeaders: {
						"X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
					},
					url : 'order/start', 
					method : 'post', 
					params : { 
					orderId :record.get('orderId')//record.data.orderId
					}, 
					success: function(response, options) {
						var json = Ext.util.JSON.decode(response.responseText);
						if(json.success){
							Ext.Msg.alert('操作成功', json.msg, function() {
								grid.getStore().reload();
							});
						}else{
							 Ext.Msg.alert('操作失败', json.msg);
						}
					}
				});
			}
		}else{
        	Ext.Msg.alert('提示',"请选择一行数据进行编辑!");
        }
    },
	onClickBackReasonButton:function(btn) {		
		var grid = btn.up('gridpanel');//获取Grid视图
		var selModel = grid.getSelectionModel();//获取Grid的SelectionModel
		if (selModel.hasSelection()) {//判断是否选中记录
			if ((selModel.getSelection()[1]) != null){
				Ext.Msg.alert('提示',"请选择其中一行数据进行编辑!");
			}else{
			    var record = selModel.getSelection()[0];//获取选中的第一条记录
			    //创建修改window和form
			    var orderGridWindow = Ext.create(Ext.apply({xtype:'backReasonWindow'}));
				//让form加载选中记录
			    orderGridWindow.down("form").getForm().loadRecord(record);
			}
		}else{
        	Ext.Msg.alert('提示',"请选择一行数据进行编辑!");
        }
	}
});